import dayjs from "../../libs/dayjs"
import { AstContext } from "../context"
import { AstVisitor } from "../visitor"
import { RedPacketInfo, redPacketInfoVisitor } from "./redPacketInfoVisitor"

export class RedPacketInfoListVisitor extends AstVisitor {
    visitImageView(node: AutoJs.UiObject, ctx: AstContext): boolean {
        const id = node.id()
        if (id) {
            if (id.endsWith("live_red_packet_container_close_view")) {
                ctx.set('closeRedPacket', () => {
                    node.click()
                    node.recycle()
                })
                return true;
            }
        }
        return false;
    }
    visitViewPager(node: AutoJs.UiObject, ctx: AstContext): boolean {
        const id = node.id()
        if (id) {
            if (id.endsWith('live_red_packet_container_view_pager')) {
                let redPacketList: RedPacketInfo[] = []
                ctx.eachChild(node, it => {
                    const ctx = new AstContext(dayjs)
                    redPacketInfoVisitor.visit(it, ctx)
                    const redPacketInfo = ctx.all()
                    if (Object.keys(redPacketInfo).length > 0) {
                        redPacketList = [
                            ...redPacketList,
                            redPacketInfo
                        ]
                    }
                })
                ctx.set('redPacketList', redPacketList)
            }
        }
        return false
    }
}
export const redPacketListInfoVisitor = new RedPacketInfoListVisitor()